package suites.deeplink {
	import org.splink.deepsplink.interceptor.IInterceptor;
	import suites.AbstractConfig;
	import suites.stub.InterceptorStub;

	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.fail;
	import org.splink.deepsplink.page.params.PageParameterUtil;

	/**
	 * @author Max Kugland
	 */
	public class InterceptorTest extends AbstractDeeplinkControllerTest {

		[Test]
		public function startup() : void {
			var gotoFnc : Function = function(id : String, params : Array): void {
				assertEquals(0, params.length);
				assertEquals(AbstractConfig.HOME, id);
			};
			
			var count : uint = 0;
			var interceptorFnc : Function = function(id : String, params : Array): Boolean {
				assertEquals(0, params.length);
				assertEquals(AbstractConfig.HOME, id);
				count++;
				return false;
			};
			
			createCtrl(gotoFnc);
			_ctrl.addInterceptor(new InterceptorStub(interceptorFnc));
			_ctrl.start();
			
			assertEquals(1, count);
			assertEquals(1, _ctrl.history.length);
		}

		[Test]
		public function multiGoto() : void {
			var results : Array = ["1","2","3","4"];
			
			var count : uint = 0;
			var interceptorFnc : Function = function(id : String, params : Array): Boolean {
				assertEquals(results[count], PageParameterUtil.constructId(id, params));
				count++;
				return false;
			};
			
			createCtrl();
			_ctrl.addInterceptor(new InterceptorStub(interceptorFnc));
			_ctrl.start();
			
			for (var i : int = 1;i < pageCount;i++) {
				_ctrl.gotoPage(i.toString());
			}
			assertEquals(results.length, count);
			assertEquals(results.length, _ctrl.history.length);
		}
		
		[Test]
		public function interceptOne() : void {
			var count : uint = 0;
			var interceptorFnc : Function = function(id : String, params : Array): Boolean {
				count++;
				return (id == AbstractConfig.HOME) ? false : true;
			};
			
			createCtrl();
			_ctrl.addInterceptor(new InterceptorStub(interceptorFnc));
			_ctrl.start();
			
			_ctrl.gotoPage("invalid");
			
			assertEquals(2, count);	
			assertEquals(1, _ctrl.history.length);	
		}
		
		[Test]
		public function interceptAll() : void {
			var interceptorFnc : Function = function(id : String, params : Array): Boolean {
				return true;
			};
			
			createCtrl();
			_ctrl.addInterceptor(new InterceptorStub(interceptorFnc));
			_ctrl.start();
			
			_ctrl.gotoPage("2");			_ctrl.gotoPage("2");			_ctrl.gotoPage("2");			_ctrl.gotoPage("2");
			
			assertEquals(0, _ctrl.history.length);	
		}
		
		[Test]
		public function interceptFirstOfTwoInterceptors() : void {
			var interceptorFnc1 : Function = function(id : String, params : Array): Boolean {
				return true;
			};
			var interceptorFnc2 : Function = function(id : String, params : Array): Boolean {
				fail("first interceptor should prevent the execution of the second interceptor");
				return false;
			};
			
			createCtrl();
			
			_ctrl.addInterceptor(new InterceptorStub(interceptorFnc1));			_ctrl.addInterceptor(new InterceptorStub(interceptorFnc2));
			_ctrl.start();
			
			_ctrl.gotoPage("2");
			assertEquals(0, _ctrl.history.length);	
		}
		
		[Test]
		public function remove() : void {
			var interceptor : IInterceptor = new InterceptorStub(function(id : String, params : Array):void {
				fail("I should have been removed!");
			});
			
			createCtrl();
			_ctrl.addInterceptor(interceptor);
			_ctrl.removeInterceptor(interceptor);
			_ctrl.start();
		}
	}
}